//
//  RootTableview.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/15.
//  Copyright © 2016年 game. All rights reserved.
//

#import "RootTableview.h"
#import "UIImage+GIF.h"
#import "UIScrollView+EmptyDataSet.h"
#import "MJRefresh.h"
#import "LoginController.h"
@interface RootTableview ()<DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic,strong) NSString *imageName;
@property (nonatomic,strong) NSString *textText;
@end

@implementation RootTableview

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{

    if (self = [super initWithFrame:frame style:style]) {
        
        
        self.emptyDataSetSource = self;
        self.emptyDataSetDelegate = self;
        //这行代码必须加上，可以去除tableView的多余的线，否则会影响美观
        self.tableFooterView = [UIView new];
        self.footer.automaticallyHidden = YES;
        
        [self HudShow];

    }
    return self;
}

-(void)HudShow{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    
    hud.labelText = @"正在加载";
    UIImage *image = [UIImage sd_animatedGIFNamed:@"pika"];
    
    // 设置图片[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", @"pika.gif"]
    hud.customView = [[UIImageView alloc] initWithImage:image];
    
    
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
        
}

-(void)setIsShowImage:(BOOL)isShowImage{
    _isShowImage = isShowImage;

    [MBProgressHUD hideHUDForView:self animated:YES];
    
   
        self.imageName = @"no content";
        self.textText  = @"抱歉!暂时没有数据";
}

-(void)setIsShowFile:(BOOL)isShowFile{
    _isShowFile = isShowFile;
    
  [MBProgressHUD hideHUDForView:self animated:YES];
        self.imageName = @"network";
        self.textText  = @"网络出错,点击重新加载";

}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
    return YES;
}
//设置点击图片
//- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{ return [UIImage imageNamed:@"empty_placeholder"];}


- (void)emptyDataSetDidTapView:(UIScrollView *)scrollView{ // Do something

    if (self.isShowFile) {
        if (_TapBlok) {
            _TapBlok();
        }

    }
    
}

- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIColor clearColor];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    
    
    if (self.isShowImage) {
     return [UIImage imageNamed:self.imageName];
    }else if (self.isShowFile){
    
        return [UIImage imageNamed:self.imageName];

    
    }else{
        return nil;
    }
    
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = self.textText;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:18.0f], NSForegroundColorAttributeName: [UIColor darkGrayColor]};
    
    if (self.isShowImage) {
        return [[NSAttributedString alloc] initWithString:text attributes:attributes];
    }else if (self.isShowFile){
        return [[NSAttributedString alloc] initWithString:text attributes:attributes];
    }
    else{
        return nil;
    }

    
    
}

//- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView{
//    NSString *text = @"点击刷新或者检查您的网络";
//    NSMutableParagraphStyle *paragraph = [NSMutableParagraphStyle new];
//    paragraph.lineBreakMode = NSLineBreakByWordWrapping; paragraph.alignment = NSTextAlignmentCenter;
//    NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:14.0f], NSForegroundColorAttributeName: [UIColor lightGrayColor], NSParagraphStyleAttributeName: paragraph};
//    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
//}

//- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{
//    
//    return -self.tableHeaderView.frame.size.height/2.0f;
//
//}

#pragma mark - scrollview代理方法
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{

    [[[UIApplication sharedApplication]keyWindow]endEditing:YES];
}

@end
